import axios from "axios"
import Qs from "qs"
import local from "../utlis/local"
import {Message} from "element-ui"
axios.defaults.baseURL="http://127.0.0.1:5000"

//请求拦截器
axios.interceptors.request.use(
    function(config){
        config.headers.Authorization=local.get("sell_token")//token是登陆返回的值
        return config
    },
    function(error){
        return Promise.reject(error)
    }
)


//响应拦截器
axios.interceptors.response.use(
    function(response){
        if(response.data.hasOwnProperty("code")&&response.data.hasOwnProperty("msg")){
            let{code,msg}=response.data;
            if(code==1){
            Message.error(msg)
            }
        }
        return response.data;
    },
    function(error){
        return Promise.reject(error)
    }
)


const request=(url,data={},method='get')=>{
    return axios({
        url,
        method,
        data:method=="post"?Qs.stringify(data):"",
        params:method=="get"?data:""
    })
}


export default{
    get(url,data){
        return request(url,data,"get")
    },
    post(url,data){
        return request(url,data,"post")
    }
}